package com.neusoft.neu24.his.hisapp.dao.impl;

import com.neusoft.neu24.his.hisapp.dao.IDeptDao;
import com.neusoft.neu24.his.hisapp.entity.Dept;
import com.neusoft.neu24.his.hisapp.util.FileIO;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DeptDaoImpl implements IDeptDao {
    private   final  static  String  FILE_NAME = "dept.json";

    private static      List<Dept> DEPT_LIST  = new ArrayList<>();
    static {
        DEPT_LIST = (List<Dept>)  FileIO.readObjectFromJsonFile(FILE_NAME ,DEPT_LIST.getClass() );
    }


    @Override
    public int addDept(Dept dept) throws IOException {
        if ( DEPT_LIST ==null ||  DEPT_LIST.size() == 0){
            DEPT_LIST = (List<Dept>)  FileIO.readObjectFromJsonFile(FILE_NAME ,DEPT_LIST.getClass() );
        }
        DEPT_LIST.add(dept);
        long l = FileIO.writeObjectAsJsonFile(DEPT_LIST,FILE_NAME);
        return (int )l ;
    }
}
